package org.apache.struts.ehr.patient_details.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts.ehr.medical_records.model.HL7Message;
import org.apache.struts.ehr.medical_records.model.ReplyMessage;
import org.apache.struts.ehr.patient_details.model.PatientDetails;
import org.apache.struts.ehr.patient_details.model.PatientEmail;
import org.apache.struts.ehr.patient_details.model.PatientTelephone;

import com.earogya.Constants;
import com.earogya.communicate.Communicator;
import com.earogya.logic.patient_details.CreateADTA08;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * Action for Update Patient
 * 
 * @author Ureka
 * 
 */
public class UpdatePatientFormAction extends ActionSupport {
	private ReplyMessage replyMessage;

	private PatientDetails patientDetails;
	private List<PatientDetails> patientDetailsList;
	private String email;
	private String telephoneNo;
	private HL7Message message;

	
	/**
	 * 
	 * Execute method for Update patient form action
	 * 
	 */
	public String execute() throws Exception {
		patientDetails.getPatientEmailList().add(new PatientEmail());
		patientDetails.getPatientEmailList().get(0).setEmail(email);
		patientDetails.getPatientTelephoneList().add(new PatientTelephone());
		patientDetails.getPatientTelephoneList().get(0)
				.setTelephoneNo(telephoneNo);
		Map<String, Object> session = ActionContext.getContext().getSession();
		replyMessage = new ReplyMessage();
		replyMessage.setMessage(Constants.EMPTY);
		CreateADTA08 createMessage = new CreateADTA08(patientDetails);
		String msg = createMessage.createMessage(Constants.SENDING_FACILITY);
		message = new HL7Message();
		if (!msg.equals(null)) {
			Communicator c = new Communicator();
			String reply = c.sendMessage(msg);
			replyMessage.setMessage("Patient Updated Successfully");
		} else {
			message.setMessage("empty");
		}
		return SUCCESS;
	}

	/**
	 * 
	 * validate the user input
	 */
	public void validate() {
		if (patientDetails.getPatient().getGivenName().length() == 0) {
			addFieldError("patientDetails.patient.givenName",
					"Given Name required");
		}
		if (patientDetails.getPatient().getNicNo().length() != 0) {
			if (!this.isCorrectNICNo(patientDetails.getPatient().getNicNo())) {
				addFieldError("patientDetails.patient.nicNo",
						"Incorrect NIC No Format");
			}
		}
		if (patientDetails.getPatientTelephoneList().size() != 0) {
			if (patientDetails.getPatientTelephoneList().get(0)
					.getTelephoneNo().length() != 0) {
				if (!this.isCorrectTelephoneNumber((patientDetails
						.getPatientTelephoneList().get(0).getTelephoneNo()))) {
					addFieldError(
							"patientDetails.patientTelephoneList.telephoneNo",
							"Incorrect Telephone Number Format");
				}
			}
		}

		if (email.length() != 0) {
			if (!this.isCorrectEmailAddress(email)) {
				addFieldError("email", "Incorrect Email Address Format");
			}

		}
	}

	/**
	 * 
	 * Check whether the given string is in correct NIC format
	 * @param nic
	 * @return
	 */
	private Boolean isCorrectNICNo(String nic) {
		Boolean isCorrectNicNo = false;
		int length = nic.length();
		if (length == 10 && (nic.charAt(9) == 'v' || nic.charAt(9) == 'V'|| nic.charAt(9) == 'x'|| nic.charAt(9) == 'X')) {
			isCorrectNicNo = true;
		}
		return isCorrectNicNo;
	}

	/**
	 * Check whether the given string is in correct telephone number format
	 * 
	 * @param number
	 * @return
	 */
	private Boolean isCorrectTelephoneNumber(String number) {
		Boolean isCorrectNumber = false;
		String[] numbers = number.split(",");
		for (int i = 0; i < numbers.length; i++) {
			String num = numbers[i].trim();
			if (num.matches("[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]")) {
				isCorrectNumber = true;
			} else if (num
					.matches("[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9]")) {
				isCorrectNumber = true;
			}
		}
		return isCorrectNumber;
	}

	/**
	 * 
	 * Check whether the given string is in corrct email format
	 * @param email
	 * @return
	 */
	private Boolean isCorrectEmailAddress(String email) {
		Boolean isCorrectEmail = false;
		if (email.matches("[0-9a-z]+@[a-z]+.[a-z]+")) {
			isCorrectEmail = true;
		}
		return isCorrectEmail;
	}

	public List<PatientDetails> getPatientDetailsList() {
		return patientDetailsList;
	}

	public void setPatientDetailsList(List<PatientDetails> patientDetailsList) {
		this.patientDetailsList = patientDetailsList;
	}

	public HL7Message getMessage() {
		return message;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getTelephoneNo() {
		return telephoneNo;
	}

	public void setTelephoneNo(String telephoneNo) {
		this.telephoneNo = telephoneNo;
	}

	public PatientDetails getPatientDetails() {
		return patientDetails;
	}

	public void setPatientDetails(PatientDetails patientDetails) {
		this.patientDetails = patientDetails;
	}

	public ReplyMessage getReplyMessage() {
		return replyMessage;
	}

	public void setReplyMessage(ReplyMessage replyMessage) {
		this.replyMessage = replyMessage;
	}

}
